package net.kem.android.positioner.custom;

import net.kem.android.positioner.MessageObject;
import net.kem.android.positioner.PositionerService;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class ServerMessageSwitchButton extends ServerMessageSwitch {
	protected ToggleButton toggleButton;

	public ServerMessageSwitchButton(PositionerService.LocalBinder binder, Context context, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject, boolean isButtonEnabled) {
		this(binder, context, (AttributeSet)null, imageOffResouceId, imageOnResouceId, messageObject, isButtonEnabled);
	}
	public ServerMessageSwitchButton(PositionerService.LocalBinder binder, Context context, AttributeSet attrs, int imageOffResouceId, int imageOnResouceId, MessageObject messageObject, boolean isButtonEnabled) {
		super(binder, context, attrs, imageOffResouceId, imageOnResouceId, messageObject);

		boolean isOn = !messageObject.getState().equals("0");
		toggleButton = new ToggleButton(context, attrs);
		toggleButton.setChecked(isOn);
		setState(toggleButton, isOn, false);
		if(isButtonEnabled) {
			toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					buttonView.getId();
					setState(buttonView, isChecked, true);
				}
			});
		} else {
			toggleButton.setEnabled(false);
		}
		addView(toggleButton);

	}

	public boolean isChecked() {
		return toggleButton.isChecked();
	}

	public void setState(View source, boolean checked, boolean doSend) {
		if(source != toggleButton) {
			toggleButton.setChecked(checked);
		} else {
			((MessageObject)namable).setState(checked? "1": "0");
		}
		super.setChecked(checked, doSend);
	}
}